﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Biz;
using Entity;
using UtilLibrary;

namespace TaskMgr
{
    public partial class frmTaskList : Form
    {
        public frmTaskList()
        {
            try
            {
                InitializeComponent();
            }
            catch (Exception ex)
            {
                Console.Write(ex.Message);
            }
            
        }

        private void frmTaskList_Load(object sender, EventArgs e)
        {
            try
            {
                // 设置单元格（cell）的名称和实体类的属性一致
                foreach (DataGridViewColumn item in this.dgTaskList.Columns)
                {
                    item.Name = item.DataPropertyName;
                }
            }
            catch (Exception ex)
            {
                Console.Write(ex.Message);
            }

            DisplayAllTask();
        }

        private void DisplayAllTask()
        {
            try
            {
                List<BizTask> tasks = TaskService.GetAllEntities();
                this.dgTaskList.DataSource = tasks;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }

        private int? clickRowIndex = null;

        private void dataGridView1_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            DataGridView dgv = (DataGridView)sender;
            if (e.Button == System.Windows.Forms.MouseButtons.Right)
            {
                Point point = dgv.PointToClient(Cursor.Position);
                this.cmsMenu.Show(dgv, point);

                this.clickRowIndex = e.RowIndex;
            }
        }

        private void tsmComplete_Click(object sender, EventArgs e)
        {
            if (this.clickRowIndex.HasValue)
            {
                DataGridViewCellCollection cells = this.dgTaskList.Rows[this.clickRowIndex.Value].Cells;

                int id = int.Parse(cells["Id"].Value.ToString());
                BaseBizEntity entity = TaskService.GetBizEntityByID(id, typeof(BizTask));
                if (entity != null)
                {
                    TaskService service = new TaskService((BizTask)entity);
                    service.Next();
                }

                DisplayAllTask();
            }
            else
            {
                MessageBox.Show("请选择一行！");
            }
        }

        private void newTSMenuItem_Click(object sender, EventArgs e)
        {
            frmEditTask frm = new frmEditTask();
            frm.ShowDialog();
        }

        private void bizTaskListBindingSource_CurrentChanged(object sender, EventArgs e)
        {

        }

    }
}
